<?php
if (!defined('MVC_ROOT_DIR'))  die('Hacking attempt');
Files::loadPlugin('Db.class.php');
class Mysql extends Db {
	public $fileLog = 0;//是否开启写入文件日志
	public $databaseLog = 0;//是否开启写入数据日志
	public $selectLog = 0;//是否开启查询日志
	public $insertLog = 0;//是否开启插入日志
	public $updateLog = 0;//是否开启更新日志
	public $deleteLog = 0;//是否开启删除日志
	public $pre;//表前缀
	public $dbLogTable='dblog';//数据库操作日志表
	
	public function __construct($dbConfig='mysql'){			
		$dbArray = Files::configLoad($dbConfig,MVC_ROOT_DIR.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'db.inc');
		if ($dbArray==false){
			errors(1001);
		}
		$logArray = Files::configLoad('databaseLog',MVC_ROOT_DIR.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'db.inc');
		$this->fileLog = $logArray['file_log'];
		
		$this->databaseLog = $logArray['database_log'];
		if ($this->databaseLog){
			$this->selectLog = $logArray['select_log'];
			$this->insertLog = $logArray['insert_log'];
			$this->updateLog = $logArray['update_log'];
			$this->deleteLog = $logArray['delete_log'];
		}
		$this->pre    =   $dbArray['table_prefix'];
		$dbHost       =   $dbArray['host'];
		$dbUser       =   $dbArray['user'];
		$dbPass       =   $dbArray['password'];
		$dbDatabase   =   $dbArray['database'];
		$dbChar       =   $dbArray['charset'];
		$dbCacheTime  = $dbArray['cachetime'];		
		return  parent::connect($dbHost,$dbUser,$dbPass,$dbDatabase,$dbChar,0,0,$dbCacheTime);
	}
	
	public function selectOne($sql){
		$this->log($sql,$this->selectLog);
		return $this->getOne($sql);
	}
	public function selectOneCache($sql){
		$this->log($sql,$this->selectLog);
		return $this->getOneCached($sql);
	}	
	//查询一行
	public function selectRow($sql){
		//$this->log($sql,$this->selectLog);
		return $this->getRow($sql);
	}
	//从缓存中查询一行
	public function selectRowCache($sql){
		$this->log($sql,$this->selectLog);
		return $this->getRowCached($sql);
	}		
	public function selectAll($sql){
		$this->log($sql,$this->selectLog);
		return $this->getAll($sql);
	}
	public function selectAllCache($sql){
		$this->log($sql,$this->selectLog);
		return $this->getAllCached($sql);
	}	
	
	public function update($sql) {	
		$this->log($sql,$this->updateLog);
		$res = $this->query($sql);		
		return $res;
	}
	public function updateData($table,$data,$where){
		$set = ' SET ';
		$count=count($data);
		$i=1;
		foreach ($data as $k=>$v){
			if ($i<$count){
				$set.="  `$k`= '$v', ";
			}  else {
				$set.="  `$k`= '$v' ";
			}
			$i++;
		}
		
		$sql = "UPDATE $table $set $where ";
		$res = $this->query($sql);		
		return $res;
		
	}
	public function delete($sql)
	{
		$this->log($sql,$this->deleteLog);
		return $this->query($sql);
	}
	
	public function insert($table, $data, $return_insert_id = false) 
	{
		$sql = $this->implodeFieldValue($data);
		$cmd = 'INSERT INTO';			
		$sql = "$cmd $table SET $sql";
		$this->log($sql,$this->insertLog);
		$return = $this->query($sql);
		return $return_insert_id ? parent::insert_id() : $return;

	}
	
	public function log($sql,$v)
	{
		if ($v){
			if ($this->fileLog){				
				$logIp = Basic::getClientIp();
				Files::logWrite($logIp.' sql:'.$sql,'database');
			}
			if ($this->databaseLog) {
				$this->dbLog($sql);
			}
		}
		
	}
	public function dbLog($msg)
	{
		$user = @$_SESSION['user'];		
		$userId = @$user['user_id'];
		$userName = @$user['user_name'];
		$logIp = Basic::getClientIp();
		$time = time();
		$msg = str_replace("'","''",$msg);
		$sql = " CREATE TABLE IF NOT EXISTS `".$this->dbLogTable."` ("
			 . " `log_id` int(11) NOT NULL AUTO_INCREMENT , "
			 . " `log_ip` VARCHAR( 15 ) NOT NULL ,"
		     . " `log_info` VARCHAR( 255 ) NOT NULL ,"
			 . " `log_time` int(10)  NOT NULL ,"			
			 . "  PRIMARY KEY (`log_id`)  "
			 . " ) ENGINE = memory CHARACTER SET utf8 COLLATE utf8_general_ci" ;	
			 
		$this->query($sql);
		$sql = " INSERT INTO ". $this->dbLogTable		     
		     . " (log_ip,log_info,log_time)"
		     . " VALUES "
		     . " ('$logIp','$msg',$time)";			
		return $this->query($sql);
	}
	public function implodeFieldValue($array, $glue = ',') {
		$sql = $comma = '';
		foreach ($array as $k => $v) {
			$sql .= $comma."`$k`='$v'";
			$comma = $glue;
		}
		return $sql;
	}
	
	public function __destruct(){
		return parent::close();
	}
}


